<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$cangid=$_RQ['cangid'];
	if(!$cangid){
		exi('请选择挂卖商品！','error');
	}
	$this->session('cangid',$cangid);
	$cang=pdo_get('jy_guashop_goods',array('id'=>$cangid,'uniacid'=>$_SESSION['uniacid']));
	$return['cang']=$cang;
	$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
	$mymember=pdo_get('jy_guashop_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$member=array_merge($member,$mymember);
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$return['member']=$member;
	$nowitem=pdo_get('jy_guashop_yao',array('cangid'=>$cangid,'uniacid'=>$_SESSION['uniacid'],'status'=>0),'','startime asc');
	$nowitem['jian_jinmax']=$nowitem['jian_jinmax']?:$cang['jian_jinmax'];
	$return['nowitem']=$nowitem;
	$isnum=pdo_count('jy_guashop_yao',array('cangid'=>$cangid,'uniacid'=>$_SESSION['uniacid'],'status >='=>1));
	$return['lastnum']=$cang['jian_num']-$isnum;
	$ai_yuenum=$this->setting['ai_yuenum'];
	$return['yaonum']=$ai_yuenum>$return['lastnum']?$return['lastnum']:$ai_yuenum;
	$aiset=pdo_get('jy_guashop_aiset',array('status'=>array(0,1),'cangid'=>$cangid,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if(!$aiset){
		$aiset=array('baojifen'=>0,'baobei'=>1,'baobei'=>1,'lastyaonum'=>$return['yaonum']);
	}
	$return['aiset']=$aiset;
	$return['list']=array();
	set_html('title','我的机器人');
	exi($return);
}
// 完成下单
if($_RQ['op']=='post'){
	$post=get_postdata();
	$cangid=$this->session('cangid');
	$post['onefee']=intval($post['onefee']);
	if(!$post['onefee']){
		exi('请输入要投入茶豆额度！','error');
	}
	if($post['lastyaonum']<$post['yaonum']){
		exi('最多可预约'.$post['lastyaonum'].'期！','error');
	}
	$team=pdo_get('jy_guashop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$cangid));
	$oneallfee=pdo_getsum('jy_guashop_aiset',array('uniacid'=>$_SESSION['uniacid'],'cangid'=>$cangid),'oneallfee');
	if($oneallfee>$team['jian_jinum']){
		exi('该挂卖商品的机器人预约已满！','error');
	}
	$nowitem=pdo_get('jy_guashop_yao',array('cangid'=>$cangid,'uniacid'=>$_SESSION['uniacid'],'status'=>0),'','startime asc');
	$jian_jinmax=$nowitem['jian_jinmax']?:$team['jian_jinmax'];
	if($post['onefee']>$jian_jinmax && $this->setting['jian_mid']!=$_SESSION['uid']){
		exi('投入额度不能大于'.$jian_jinmax.'！','error');
	}
	
	if($post['onefee']<$team['jian_jinmin'] && $this->setting['jian_mid']!=$_SESSION['uid']){
		exi('投入茶豆额度不能小于'.$team['jian_jinmin'].'！','error');
	}
	$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
	if($post['baobei']>1){
		$isfee=pdo_getsum('jy_guashop_aiset',array('cangid'=>$cangid,'uniacid'=>$_SESSION['uniacid']),'oneallfee');
		$lastfee=$yao['alljifee']-$isfee;
		if($post['jifen']>$lastfee){
			exi('当前剩余'.$lastfee.'可投入茶豆！请选择其他倍数！','error');
		}
	}
	if($member['credit4']<$post['payfee']){
		exi('您的茶豆不足，不能参与！','error');
	}
	$tag='机器人挂卖';
	$payjifen=$post['payjifen'];
	if($payjifen>$member['credit1']){
		exi('本次'.$tag.'需消耗'.$payjifen.'积分，您的积分不足，不能参与！','error');
	}
	$post['cangid']=$cangid;
	$post['paixu']=$nowitem['paixu'];
	$post['jian_xiaojinum']=$team['jian_xiaojinum'];
	$this->model('ai')->up_ailog($post);
	exi($tag.'成功！','','jy_guashop/ailog/');
}
if($_RQ['op']=='setstatus'){
	$post=get_postdata();
	$id=$_RQ['id'];
	$status=$_RQ['status'];
	pdo_update('jy_guashop_aiset',array('status'=>$status),array('id'=>$id,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	exi('操作成功！','','jy_guashop/cang/');
}